package com.cdd.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.commonutils.R;
import com.cdd.commonutils.orderVo.CourseWebVoOrder;
import com.cdd.eduservice.entity.EduCourse;
import com.cdd.eduservice.entity.chapter.ChapterVo;
import com.cdd.eduservice.entity.frontVo.CourseQueryVo;
import com.cdd.eduservice.entity.frontVo.CourseWebVo;
import com.cdd.eduservice.entity.vo.CourseInfoVo;
import com.cdd.eduservice.service.IEduChapterService;
import com.cdd.eduservice.service.IEduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/eduservice/courseFront")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private IEduCourseService eduCourseService;

    @Autowired
    private IEduChapterService eduChapterService;

    @PostMapping("pageFrontCourseList/{current}/{limit}")
    public R pageCourseList(@PathVariable long current, @PathVariable long limit,
                            @RequestBody(required = false) CourseQueryVo courseQueryVo) {
        Page<EduCourse> page = new Page<>(current,limit);
        Map<String, Object> map = eduCourseService.getFrontCourseList(page,courseQueryVo);
        return R.ok().data(map);
    }


    @GetMapping("getCourseById/{id}")
    public R getCourseById(@PathVariable String id){
        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = eduCourseService.selectInfoWebById(id);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = eduChapterService.getChapterAndVideo(id);
        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList);
    }

    // 根据课程Id查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){

        CourseInfoVo courseInfo = eduCourseService.getCourseInfo(id);
        System.out.println(courseInfo.toString());
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}
